package com.authine.cloudpivot.ext.utils.system;

import com.alibaba.cola.dto.RequestContext;
import com.authine.cloudpivot.ext.constants.PlatformConst;
import com.authine.cloudpivot.ext.dto.system.UserDto;
import com.authine.cloudpivot.ext.enums.system.ArchitectureCateEnum;
import com.authine.mvp.app.launcher.domain.SelectorFormat;
import com.authine.mvp.app.org.dto.domainmodel.UnitType;

import java.util.Objects;
import java.util.Set;

/**
 * @ClassName SessionUser
 * @Description: 当前登录用户信息
 * @Author fj
 * @Date 2021/9/1
 * @Version V1.0
 **/
public class SessionUser {
    private UserDto dto;

    /**
     * 获取当前登录用户
     * @return  登录用户信息
     */
    public UserDto get() {

        if (dto == null) {
            String userId = RequestContext.getCorpLoginId();
            dto = UserUtils.getUserByUserId(userId);
        }
        return dto;
    }

    /**
     * 获取当前登录用户id
     * @return  登录用户id
     */
    public String getCurrentUserId() {
        return get().getId();
    }

    /**
     * 获取当前登录用户的组织id
     * @return  登录用户组织id
     */
    public String getCurrentOrgID() {
        return get().getOrganization_id().get(0).getId();
    }

    /**
     * 获取当前登录用户的部门id
     * @return  登录用户部门id
     */
    public String getCurrentDepartmentID() {
        return get().getDepartment_id().get(0).getId();
    }

    /**
     * 获取当前登录用户的客户端id
     * @return  当前用户登录客户端id
     */
    public String getCurrentClientID() {
//        return RequestContext.getClientId();
        return get().getClient_id();
    }

    /**
     * 当前用户是否是平台
     */
    public boolean isPm() {
        // 平台id为固定值
        return Objects.equals(this.getCurrentOrgID(), PlatformConst.ORGANIZATION_ID);
    }

    /**
     * 当前用户是否拥有供应商角色
     * @return  true：拥有 false：未拥有
     */
    public boolean isVendor() {
        Set<ArchitectureCateEnum> organizationArchitecture = RoleUtils.getOrganizationArchitecture(getCurrentOrgID());
        return organizationArchitecture.contains(ArchitectureCateEnum.VENDOR);
    }

    /**
     * 当前用户是否拥有客户角色
     * @return  true：拥有 false：未拥有
     */
    public boolean isCustomer() {
        Set<ArchitectureCateEnum> organizationArchitecture = RoleUtils.getOrganizationArchitecture(getCurrentOrgID());
        return organizationArchitecture.contains(ArchitectureCateEnum.CUSTOMER);
    }

    /**
     * 用户信息转成选人控件格式
     * @return  用户信息
     */
    public SelectorFormat getUserFormat() {
        UserDto userDto = get();
        SelectorFormat format = new SelectorFormat();
        format.setId(userDto.getId());
        format.setName(userDto.getFullname());
        format.setType(UnitType.USER.getIndex());
        return format;
    }

    /**
     * 用户信息转成选人控件格式
     * @return  用户信息
     */
    public SelectorFormat getUserFormat(String userId, String name) {
        SelectorFormat format = new SelectorFormat();
        format.setId(userId);
        format.setName(name);
        format.setType(UnitType.USER.getIndex());
        return format;
    }
}
